﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace IMIS_ERP.Web.Order_Info
{
    public partial class Search_Manager_Factory_All : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["UCode"].ToString().Substring(11, 1) == "0")
                {
                    Response.Write("<script>alert('没有权限！');location.href='../ERP_Manager/EM_Page_Main.aspx';</script>");
                }
                Bind_GV("2010-1-1","2110-1-2");
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string time1 = "2010-1-1", time2 = "2110-1-1";
            if (txthTime1.Text != "")
            {
                time1 = txthTime1.Text.Trim() + " 00:00:00";
            }
            if (txthTime2.Text != "")
            {
                time2 = txthTime2.Text.Trim() + " 23:59:59";
            }
            Bind_GV(time1, time2);
        }

        /// <summary>为GridView绑定数据
        /// 	<remark>2011-03-23 BY 河北大学信管团队</remark>
        /// </summary>
        private void Bind_GV(string strTime1, string strTime2)
        {
            string fiProvince = "", fiCity = "";
            if (Session["UType1"].ToString() == "0")
            {
                fiProvince = "%%";
                fiCity = " like '%%'";
            }
            else if (Session["UType1"].ToString() == "1")
            {
                fiProvince = Session["UProvince"].ToString();
                fiCity = " like '%%'";
            }
            else if (Session["UType1"].ToString() == "2")
            {
                fiProvince = Session["UProvince"].ToString();
                fiCity = "='" + Session["UCity"].ToString() + "'";
            }
            IMIS_ERP.BLL.Factory_Info bll = new BLL.Factory_Info();
            SqlDataReader dr = bll.GetfiID_M(fiProvince, fiCity);
            IMIS_ERP.BLL.Order_Info bll1 = new IMIS_ERP.BLL.Order_Info();
            DataSet ds = bll1.GetFiSumAll(fiProvince, fiCity, "0", strTime1,strTime2);
            DataView dv1 = ds.Tables[0].DefaultView, dv2 = ds.Tables[1].DefaultView, dv3 = ds.Tables[2].DefaultView, dv4 = ds.Tables[3].DefaultView;
            DataTable dt = new DataTable();
            dt.Columns.Add("oiFiID");
            dt.Columns.Add("TaskSum");
            dt.Columns.Add("DoneSum");
            dt.Columns.Add("HandSum");
            dt.Columns.Add("InstallSum");
            decimal sum1 = 0, sum2 = 0, sum3 = 0, sum4 = 0;
            if (ds.Tables[0].Rows.Count > 0)
            {
                while (dr.Read())
                {
                    dv1.RowFilter = dv2.RowFilter = dv3.RowFilter = dv4.RowFilter = "oiFiID='" + dr["fiID"].ToString() + "'";
                    DataTable dt1 = dv1.ToTable(), dt2 = dv2.ToTable(), dt3 = dv3.ToTable(), dt4 = dv4.ToTable();
                    dt.Rows.Add(dr["fiID"].ToString(), dt1.Rows.Count > 0 ? dt1.Rows[0]["oiSum"].ToString() : "0", dt2.Rows.Count > 0 ? dt2.Rows[0]["oiSum"].ToString() : "0", dt3.Rows.Count > 0 ? dt3.Rows[0]["oiSum"].ToString() : "0", dt4.Rows.Count > 0 ? dt4.Rows[0]["oiSum"].ToString() : "0");
                    sum1 += Convert.ToDecimal(dt1.Rows.Count > 0 ? dt1.Rows[0]["oiSum"].ToString() : "0");
                    sum2 += Convert.ToDecimal(dt2.Rows.Count > 0 ? dt1.Rows[0]["oiSum"].ToString() : "0");
                    sum3 += Convert.ToDecimal(dt3.Rows.Count > 0 ? dt1.Rows[0]["oiSum"].ToString() : "0");
                    sum4 += Convert.ToDecimal(dt4.Rows.Count > 0 ? dt1.Rows[0]["oiSum"].ToString() : "0");
                }
            }
            dt.Rows.Add("总计", sum1, sum2, sum3, sum4);
            GV.DataSource = dt;
            GV.DataBind();
        }

        /// <summary>响应GridView事件：创建数据行时
        /// 	<remark>2010-12-08 BY 河北大学信管团队</remark>
        /// </summary>
        protected void GV_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //为数据行增加鼠标移动到行特性
                e.Row.Attributes.Add("onmouseover", "c=style.backgroundColor;this.style.backgroundColor='" + IMIS_ERP.BLL.Assistant.getColor1 + "'");
                //为数据行增加鼠标离开行特性
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
            }
        }
    }
}